/**
 * 店铺验证类
 * @type {*|exports|module.exports}
 */
var AV = require('leanengine');
// 状态  0代表flase，1代表true
var rState="";	
var rCode="";
// 如有错误或异常，该列才有值/
var rMsg="";
// 通用返回对象
var rResult = "";
var err;
var common   = require( '../cloud/common' );
/**
 * 申请身份认证
 * @param request
 * @param callback
 */
function applyVerify(request,callback){	
	var name=request.params.name;//开卡人姓名
	var tel=request.params.tel;//手机号
	var identityCardNo=request.params.identityCardNo;//身份证号
	var holdIdentityCard=request.params.holdIdentityCard;//手持身份证正面照片
	if(name==null||name==undefined)
		name="";
	if(tel==null||tel==undefined)
		tel="";
	if(identityCardNo==null||identityCardNo==undefined)
		identityCardNo="";
	if(holdIdentityCard==null||holdIdentityCard==undefined)
		holdIdentityCard="";
	tel=tel.toString();
	identityCardNo=identityCardNo.toString();
	name=name.replace(/s/g, "");
	tel=tel.replace(/s/g, "");
	identityCardNo=identityCardNo.replace(/s/g, "");
	holdIdentityCard=holdIdentityCard.replace(/s/g, "");
	verifyStatus(request.user.id,function(error,verifyObj){
		if(error){
			return callback(null,JSON.stringify(common.getErrArr(error)));
		}else{

			//修改审核资料
			if(verifyObj!=null){
				if("1"==verifyObj.get("status")||""==verifyObj.get("status")||null==verifyObj.get("status")||undefined==verifyObj.get("status"))
					return callback(null,JSON.stringify(common.getErrArr("你已经审核通过")));
			}
				var Accountverify=common.getEntity("Shop_account_verify");
				var accountverify = new Accountverify();

			accountverify.set("userOId",request.user.id);
			if(name!=""){
				accountverify.set("name",name);		
			}
			if(identityCardNo!=""){
				accountverify.set("identityCardNo",identityCardNo);
			}
			if(tel!=""){
				accountverify.set("tel",tel);
			}
			if(holdIdentityCard!=""){
				accountverify.set("holdIdentityCard",{"__type":"Pointer",className:"_File",objectId:holdIdentityCard});
			}
			accountverify.set("status","3");
			accountverify.set("shopStatus","3");
			accountverify.save(null, {
				success : function(newObj) {
					return callback(null,JSON.stringify(common.getReturnArr("1","1000","","")));
				},error:function(errorObj,error){
					return callback(null,JSON.stringify(common.getErrArr(error)));
				}
			})
		}
	});
}

/**
 * 查询是否通过身份认证
 * @param request
 * @param callback
 */

function checkIsVerify(request,callback){	
	var result={};
	verifyStatus(request.user.id,function(error,verifyObj){
		if(error){
			result["status"]="0";
			result["reason"]=error;
			var returnArr=common.getReturnArr("","1000","",result);
			return callback(null,JSON.stringify(returnArr));
		}else{
			if(verifyObj!=null){
				var status=verifyObj.get("status");
				if(status==null||status==undefined)
					status="";
				result["status"]=status;
				result["reason"]=verifyObj.get("reason");
				var returnArr=common.getReturnArr("","1000","",result);
				return callback(null,JSON.stringify(returnArr));
			}else{
				result["status"]="0";
				result["reason"]="您还未提交过审核信息，请提交";
				var returnArr=common.getReturnArr("","1000","",result);
				return callback(null,JSON.stringify(returnArr));
			}
		}
	});
}
exports.verifyStatus=verifyStatus;
/**
 * 查看是否通过身份验证
 * @param userOId
 * @param callback
 */
function verifyStatus(userOId,callback){	
	if(userOId==null||userOId==""||userOId==undefined){
		 return callback("错误的参数",null);
	}
	var query=new AV.Query('Shop_account_verify');	
	query.equalTo("userOId",userOId);
	query.first({
		 success : function(verifyObj) {			
			 return callback(null,verifyObj); 
		 },
	     error: function(error) {
			return callback(error.message,null);
		 }
	});
}
/**
 * 申请身份认证
 */
AV.Cloud.define("applyVerify",function(request, response) {
 		   if(!common.verifyLogin(request, response)){
			return response.error("登陆信息过期，请尝试重新登陆。");		
		   }else{
				  if(request.params.name==""||request.params.identityCardNo==""){
					  return response.success(JSON.stringify(common.getErrArr("请传入正确的参数！")));
				   }else if(request.params.name==undefined||request.params.identityCardNo==undefined){
					  return response.success(JSON.stringify(common.getErrArr("请传入正确的参数！")));
				   }
			     else{
			    	 applyVerify(request, function(error,returnStr) {
						if(error){
							return response.error(returnStr);
						}else{
							return response.success(returnStr);
						}
					});
			    }
			}
	});
/**
 * 查询是否通过身份认证
 */
AV.Cloud.define("checkIsVerify",function(request, response) {
 		   if(!common.verifyLogin(request, response)){
			return response.error("登陆信息过期，请尝试重新登陆。");		
		   } else{
			    	 checkIsVerify(request, function(error,returnStr) {
						if(error){
							return response.error(returnStr);
						}else{
							return response.success(returnStr);
						}
					});
			    }
			
	});


